package com.zwj.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zwj.bo.AddDataDictionaryBo;
import com.zwj.bo.DataDictionaryBo;
import com.zwj.dto.PaginationDate;
import com.zwj.dto.Result;
import com.zwj.dto.ReturnDate;
import com.zwj.entity.CourseInfoEntity;
import com.zwj.entity.DataDictionaryEntity;
import com.zwj.enums.IsDeletedEnum;
import com.zwj.mapper.DataDictionaryMapper;
import com.zwj.service.DataDictionaryService;
import com.zwj.utils.DataCheckUtils;
import com.zwj.utils.DateUtils;
import com.zwj.utils.PaginationUtils;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DataDictionaryServiceImpl extends ServiceImpl<DataDictionaryMapper, DataDictionaryEntity> implements DataDictionaryService {
    private final DataDictionaryMapper dataDictionaryMapper;

    public DataDictionaryServiceImpl(DataDictionaryMapper dataDictionaryMapper){
        this.dataDictionaryMapper = dataDictionaryMapper;
    }

    @Override
    public Result addDataDictionary(AddDataDictionaryBo addDataDictionaryBo, String userId) {
        DataDictionaryEntity dataDictionaryEntity = new DataDictionaryEntity();
        dataDictionaryEntity.setCode(addDataDictionaryBo.getCode());
        dataDictionaryEntity.setName(addDataDictionaryBo.getName());
        dataDictionaryEntity.setValue(addDataDictionaryBo.getValue());
        dataDictionaryEntity.setCreateBy(userId);
        dataDictionaryEntity.setCreateTime(DateUtils.getCurrentTime());
        dataDictionaryEntity.setIsDeleted(IsDeletedEnum.NO.getKey());
        int row = dataDictionaryMapper.insert(dataDictionaryEntity);
        //判断插入结果
        if (row == 0) {
            return Result.error().message("添加课程失败");
        }
        return Result.ok();
    }

    @Override
    public Result selectCourseInfo(DataDictionaryBo dataDictionaryBo) {
        DataCheckUtils.isValid(PaginationUtils.isPagingParameters(dataDictionaryBo.getNumber(), dataDictionaryBo.getPage()), "分页参数错误!");
        int page = dataDictionaryBo.getPage();
        dataDictionaryBo.setPage(PaginationUtils.getPageIndex(dataDictionaryBo.getPage(), dataDictionaryBo.getNumber()));
        List<Object> result = dataDictionaryMapper.selectDataDictionary(dataDictionaryBo);//查询项目组评分 mybatis
        int sum = ((List<Integer>) result.get(1)).get(0);
        //记录总数为0
        if (sum == 0) return Result.error().message("查询结果为空");
        PaginationDate paginationDate = PaginationUtils.getPaginationDate(dataDictionaryBo.getNumber(), page, sum);
        //当前页大于总页
        if (PaginationUtils.isValid(paginationDate))
            return Result.error().message("分页参数错误!");
        List<DataDictionaryEntity> dataDictionaryEntities =  (List<DataDictionaryEntity>) result.get(0);
        return Result.ok().data(new ReturnDate(paginationDate,dataDictionaryEntities ));
    }
}
